package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.jws.Oneway;
import java.util.List;
import java.util.Map;

/**
 * 预约列表
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    OrderService orderService;

    @Autowired
    JedisPool jedisPool;


    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        System.out.println(queryPageBean);
        try {
            return orderService.pageQuery(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //查询所有预约列表
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Map<String, Object>> list = orderService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);//查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);//查询失败
        }
    }


    //新增预约数据
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String, Object> map, Integer[] setmealIds) {
        try {
            //add
            for (Integer setmealId : setmealIds) {
                System.out.println(setmealId);
            }
            orderService.add(map, setmealIds);
            return new Result(true, "新增列表成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增列表失败");
        }
    }

    //确定预约
    @RequestMapping("updateOrderStatus")
    public Result updateOrderStatus(Integer id) {
        try {
            orderService.updateOrderStatus(id);
            return new Result(true, "确认预约成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "确认预约失败");
        }
    }

    //取消预约
    @RequestMapping("updateOrderStatus2")
    public Result updateOrderStatus2(Integer id) {
        try {
            orderService.updateOrderStatus2(id);
            return new Result(true, "取消预约成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "取消预约失败");
        }
    }

    //多选成功预约
    @RequestMapping("updateOrderStatusSelection")
    public Result updateOrderStatusSelection(Integer[] ids) {
        try {
            orderService.updateOrderStatusSelection(ids);
            return new Result(true, "多条确认预约成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "多条确认预约失败");
        }
    }

    //多选取消预约
    @RequestMapping("updateOrderStatusSelection2")
    public Result updateOrderStatusSelection2(Integer[] ids) {
        try {
            orderService.updateOrderStatusSelection2(ids);
            return new Result(true, "多条取消预约成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "多条取消预约失败");
        }
    }

    //预约取消发送短信
    @RequestMapping("/send4Order")
    public Result send4Order(String phoneNumber) {
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        try {
            //发送短信
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, phoneNumber, code.toString());
        } catch (ClientException e) {
            e.printStackTrace();
            //发送短信失败
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //验证码发送成功
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
